package com.minimoment.util.javamail;


import com.minimoment.util.common.CommomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by etund on 2014/10/29.
 */
@Component
public class JavaMailActive {
    @Autowired
    private JavaMailSenderImpl mailSender;

    public void MailActive(String register, String email) {
        try {
            System.out.println(mailSender);
            System.out.println(mailSender.getUsername());
//        href 有IP地址+：8887+动作
            String ip = CommomUtils.getRealIp();
//建立邮件消息,发送简单邮件和html邮件的区别
            MimeMessage mailMessage = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
            messageHelper.setFrom(mailSender.getUsername());
            System.out.println(email+"========email========");
            messageHelper.setTo(email);
            messageHelper.setSubject("点击下面信息激活你的账号");
//            ？register="+register
            String href = "http://"+ip+":8887"+"/user_registerConfirm.do?register="+register;
            System.out.println(href + "---------");
            String tits = "点击下面链接激活";
            tits = new String(tits.getBytes("iso-8859-1"),"utf-8");
            messageHelper.setText("<html><body><a href = " + href + ">"+tits + href + "</a></body></html>", true);
            Properties properties = new Properties();
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.timeout", "25000");
            mailSender.send(mailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
